home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Software Vault: The Gold Collection
/
Software Vault - The Gold Collection (American Databankers) (1993).ISO
/
cdr11
/
pdox693.zip
/
TI1123.ASC
< prev
next >
Wrap
Text File
|
1992-10-19
|
7KB
|
265 lines
PRODUCT : Paradox NUMBER : 1123
VERSION : 4.0
OS : DOS
DATE : October 19, 1992 PAGE : 1/4
TITLE : An Introduction to DYNARRAY Branching
Prerequisites:
This Tech Info article assumes a basic working knowledge of the
following commands: PROC, EXECPROC, DYNARRAY, SWITCH and
SHOWPOPUP. An intermediate to advanced level of PAL knowledge is
also recommended for full comprehension of the concepts involved.
The Concept:
The concept of DYNARRAY Branching has its origin in the more
direct PAL command: SWITCH. The standard SWITCH/CASE statement
is a method for evaluating a number of different logical
conditions (CASES) and performing the actions associated with the
first CASE in which the condition is true.
Here is a simple example of a SWITCH statement which runs a
different procedure depending on which menu selection is chosen.
This example assumes that the three procedures C1, C2, and C3
have already been defined and reside in memory or a library in an
Autolib path.
SHOWPOPUP "MenuList" CENTERED
"Choice1" : "First choice" : "C1",
"Choice2" : "Second choice" : "C2",
"Choice3" : "Third choice" : "C3"
ENDMENU
TO MenuSelection
SWITCH
CASE MenuSelection = "C1" : C1()
CASE MenuSelection = "C2" : C2()
CASE MenuSelection = "C3" : C3()
ENDSWITCH
With only three menu choices, the code above is succinct and
quick. But what happens when you have thirty or forty menu
choices. Not only must you define the thirty or forty procedures
but must also add new CASES to the SWITCH statement. This
technique does not lend itself to the writing of reusable code or
easy modification.
The use of the EXECPROC command can eliminate the necessity for a
lengthy SWITCH statement. Before exploring this replacement
PRODUCT : Paradox NUMBER : 1123
VERSION : 4.0
OS : DOS
DATE : October 19, 1992 PAGE : 2/4
TITLE : An Introduction to DYNARRAY Branching
technique here is a brief review of EXECPROC. This command
executes a procedure by translating a string value into a
procedure call. The procedure must have an empty parameter list.
For example the following statement
C1()
would call the procedure C1. This could also be accomplished
with the EXECPROC statement:
EXECPROC "C1"
A further replacement process would allow the procedure name
string to be derived from a variable. For instance:
ProcName = "C1"
EXECPROC ProcName
This leads to the following code which replaces the SWITCH
statement in Example 1:
SHOWPOPUP "MenuList" CENTERED
"Choice1" : "First choice on the menu" : "C1",
"Choice2" : "Second choice on the menu" : "C2",
"Choice3" : "Third choice on the menu" : "C3"
ENDMENU
TO MenuSelection
EXECPROC MenuSelection
One line of code can take the place of any number of separate
CASES.
Note: The interpreter which enables EXECPROC to convert strings
to procedure calls may take a little time to load. In some cases
EXECPROC may be slower than a short SWITCH statement. The point
at which performance improves by using EXECPROC over SWITCH may
vary from machine to machine. You will want to do some of your
own benchmarking if performance is a priority over generic
coding. The performance benefits of EXECPROC over SWITCH seems
to start around 10 possible conditions.
PRODUCT : Paradox NUMBER : 1123
VERSION : 4.0
OS : DOS
DATE : October 19, 1992 PAGE : 3/4
TITLE : An Introduction to DYNARRAY Branching
Up to this point Dynamic Arrays have not been an issue. Similar
code could have been written using Paradox 3.5. The necessity
for Dynamic Arrays comes in when you do not have a direct
correspondence between the string value returned by the menu and
the name of the procedure to be executed.
For instance, if the tag of Choice1 in the SHOWPOPUP statement
was not also the name of the procedure to be called, the EXECPROC
statement alone would not call the correct procedure. DYNARRAYs
allow a "map" to drawn between the possible returned values and
their corresponding procedures.
Here is yet another version of Example 1. This one demonstrates
this technique:
DYNARRAY ProcNames[]
ProcNames["C1"] = "Option1"
ProcNames["C2"] = "Option2"
ProcNames["C3"] = "Option3"
SHOWPOPUP "MenuList" CENTERED
"Choice1" : "First choice on the menu" : "C1",
"Choice2" : "Second choice on the menu" : "C2",
"Choice3" : "Third choice on the menu" : "C3"
ENDMENU
TO MenuSelection
EXECPROC ProcNames[MenuSelection]
The levels of substitution must be peeled away one layer at a
time to obtain a direct understanding of the execution process.
First, the variable MenuSelection is replaced with the literal
string value of the user selected menu choice ("C1" for example):
EXECPROC ProcNames["C1"]
The next substitution requires that the DYNARRAY tag is replaced
by its stored string value:
EXECPROC "Option1"
Which finally is executed as:
PRODUCT : Paradox NUMBER : 1123
VERSION : 4.0
OS : DOS
DATE : October 19, 1992 PAGE : 4/4
TITLE : An Introduction to DYNARRAY Branching
Option1()
While this may seem like a lot of code just to call a procedure,
all of the setup is up front in the DYNARRAY declaration and
assignment of values, and it is only done once. SWITCH must
evaluate which Boolean condition is true then call the
appropriate procedure. DYNARRAY Branching has the advantage of
"punching through" the execution layers to directly call a
procedure. This is why the performance of SWITCH slows down
after a certain number of cases while EXECPROC remains constant
no matter how many elements are stored in the Dynamic Array.
This technique also has serious coding advantages when used in
the context of the new Event-Driven WAIT command and the
SHOWDIALOG command. These techniques are beyond the scope of
this Tech Info article. Future articles will elaborate on these
issues.
DISCLAIMER: You have the right to use this technical information
subject to the terms of the No-Nonsense License Statement that
you received with the Borland product to which this information
pertains.